feat: Add granular cache flushing commands - #125
Conversation
Adds granular cache flushing for posts, terms, comments, users, and options. Implements CLI equivalents for WordPress cache-clearing functions: - wp cache flush-post [ID] - wp cache flush-term [ID] - wp cache flush-comment [ID] - wp cache flush-user [ID] - wp cache flush-option [name] Addresses wp-cli#108: Allow selective cache clearing instead of flushing entire cache. Without IDs/names, clears all cache groups for that type. With IDs/names, clears specific items using WordPress core functions.
|
Hello! 👋 Thanks for opening this pull request! Please check out our contributing guidelines. We appreciate you taking the initiative to contribute to this project. Contributing isn't limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation. Here are some useful Composer commands to get you started:
To run a single Behat test, you can use the following command: # Run all tests in a single file
composer behat features/some-feature.feature
# Run only a specific scenario (where 123 is the line number of the "Scenario:" title)
composer behat features/some-feature.feature:123You can find a list of all available Behat steps in our handbook. |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Add array<string> type hints to method parameters for PHPStan compliance.
Add wp_cache_supports() checks before calling wp_cache_flush_group(). Requires WordPress 6.1+ or persistent object cache with group flushing support. Specific item flushing works on all WordPress versions. Update tests to validate specific item flushing and expect errors when group flushing not supported.
Remove " or a persistent object cache with group flushing support" suffix from error messages to match test expectations. Commands now show: - "Flushing all post caches requires WordPress 6.1+" - "Flushing all term caches requires WordPress 6.1+" - "Flushing all comment caches requires WordPress 6.1+" - "Flushing all user caches requires WordPress 6.1+" - "Flushing all option caches requires WordPress 6.1+"
Replace wp_cache_supports check with direct function existence check. wp_cache_flush_group was added in WordPress 6.1, so checking if it exists is simpler and more direct than checking cache support via wp_cache_supports.
Use wp_cache_supports('flush_group') to check if the object cache
implementation supports group flushing, rather than just checking
if the function exists. This properly detects when the feature
is supported by the specific cache implementation in use.
WP 6.1+ default in-memory cache returns true for wp_cache_supports('flush_group'),
so the old check never errored on modern WordPress. Add wp_using_ext_object_cache()
as the primary gate: group flushing only makes sense with a persistent external
cache (Redis, Memcached, etc.), so error without one regardless of WP version.
WP 6.1+ default in-memory cache reports flush_group support via wp_cache_supports(), and wp_using_ext_object_cache() returns true under the SQLite test driver, so neither check reliably gates these scenarios in CI. Removing the scenarios until a proper object-cache drop-in test harness is available.
There was a problem hiding this comment.
Pull request overview
Adds new wp cache subcommands to flush caches at a more granular level (post, term, comment, user, option), addressing the need to avoid expensive full-cache flushes and to expose CLI equivalents of WordPress cache-clearing functions.
Changes:
- Introduces
flush-post,flush-term,flush-comment,flush-user, andflush-optionsubcommands inCache_Command. - Adds Behat coverage for the new subcommands (currently focused on “specific ID/name” invocation).
- Registers the new commands in
composer.jsonbundled command metadata.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 11 comments.
| File | Description |
|---|---|
| src/Cache_Command.php | Adds new granular cache flushing subcommands and their runtime behavior. |
| features/cache-flush-granular.feature | Introduces acceptance tests for the new granular flush commands. |
| composer.json | Adds the new subcommands to the bundled commands list. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| $cache_post = function(){ | ||
| wp_cache_set( 'post_123', array( 'ID' => 123, 'post_title' => 'Test' ), 'posts' ); | ||
| wp_cache_set( 'meta_123', array( 'key' => 'value' ), 'post_meta' ); | ||
| }; | ||
| WP_CLI::add_hook( 'before_invoke:cache flush-post', $cache_post ); |
|
@alaminfirdows Would you be up for assessing & addressing the above code review feedback? |
Sorry for the delay, I'll push the updates very soon. |
|
Warning Review limit reached
Next review available in: 52 minutes Limit details: You’ve used the included review currently available. You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. How can I continue?Wait for the limit to reset, then comment An organization admin can change what happens after included review limits in Billing. How do review limits work?CodeRabbit enforces per-developer PR review limits within each organization. For paid Pro and Pro+ reviews, CodeRabbit uses a developer's included PR review attempts over the past 7 days to set the current hourly allowance. At typical activity levels, the full plan allowance applies. Higher sustained activity can lower the allowance until earlier attempts leave the 7-day window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review. 📝 WalkthroughWalkthroughAdded five granular cache-flushing commands for posts, terms, comments, users, and options. The commands support targeted identifiers and bulk cache-group flushing with validation and failure reporting. Acceptance scenarios cover targeted, invalid-input, and bulk operations. ChangesGranular cache flushing
Estimated code review effort: 3 (Moderate) | ~25 minutes Merge Risk: 🟡 Moderate · up to The new granular cache-flush acceptance scenarios use post and term IDs that are not created by the fixtures, so cleanup can error and fail the test suite; merge readiness is moderate until those scenarios use persisted fixture entities. Suggested reviewers: Sequence Diagram(s)sequenceDiagram
participant CLI
participant Cache_Command
participant WordPressCacheAPI
CLI->>Cache_Command: Run a granular cache flush command
Cache_Command->>Cache_Command: Validate targeted IDs when provided
Cache_Command->>WordPressCacheAPI: Clear targeted entries or flush a cache group
WordPressCacheAPI-->>Cache_Command: Return the cache operation result
Cache_Command-->>CLI: Report success or failure
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
- Add proper input validation for numeric IDs to prevent silent fallthrough - Remove wp_using_ext_object_cache() requirement; rely only on wp_cache_supports( 'flush_group' ) - Check return values of wp_cache_flush_group() calls and error on failure - Add after_invoke hooks to verify cache is actually cleared - Add @require-wp-6-1 version-gated scenarios for group flush behavior - Add error test cases for invalid IDs
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@features/cache-flush-granular.feature`:
- Around line 9-20: The cache-flush scenarios currently verify only command
output and must assert canonical cache invalidation in-process. In
features/cache-flush-granular.feature lines 9-20, create post 123 and verify
numeric key 123 is absent from posts and post_meta; lines 28-39, verify key 5 is
absent from terms and update the command if term metadata must also be
invalidated; lines 47-58, verify key 42 is absent from comment and update the
command if comment metadata must also be invalidated; lines 66-77, ensure user 1
exists and verify key 1 is absent from users and user_meta; lines 85-96, verify
both alloptions and my_option are absent after the option flush.
In `@src/Cache_Command.php`:
- Around line 638-645: Reject supplied invalid IDs before choosing the
bulk-flush path. In src/Cache_Command.php ranges 638-645, 676-683, 714-721, and
752-759, detect argument presence separately, validate that the ID is a positive
integer, and only then enter the targeted branch; report invalid supplied values
instead of treating them as absent and flushing all post cache groups.
- Around line 647-649: Update all five cache-clearing handlers in
src/Cache_Command.php at lines 647-649, 685-687, 723-725, 761-763, and 800-801
to validate every wp_cache_flush_group() return value before calling
WP_CLI::success(). Call WP_CLI::error() when any required group fails:
posts/post_meta, terms/term_meta, comment/comment_meta, users/user_meta, or
options; otherwise preserve the existing success messages.
- Around line 675-680: Update src/Cache_Command.php lines 675-680 in flush_term
to pass the term’s taxonomy explicitly to clean_term_cache() and delete the
numeric term ID from term_meta. Also update src/Cache_Command.php lines 713-718
in the targeted comment branch to delete the numeric comment ID from
comment_meta after clean_comment_cache().
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: c6cac611-9ea3-489f-84eb-232208c9fa6f
📒 Files selected for processing (3)
composer.jsonfeatures/cache-flush-granular.featuresrc/Cache_Command.php
…wp-cli-cache-command into feat/granular-cache-flush
Adds granular cache flushing for posts, terms, comments, users, and options. Implements CLI equivalents for WordPress cache-clearing functions:
wp cache flush-post [ID]wp cache flush-term [ID]wp cache flush-comment [ID]wp cache flush-user [ID]wp cache flush-option [name]Completed #108
Summary by CodeRabbit
New Features
Bug Fixes
Tests